import http, { type ApiResponse } from '~/utils/http'

// 用户相关接口类型定义
export interface UserInfo {
  id?: number
  name: string
  school: string
  class: string
  studentId: string
  avatar?: string
  createdAt?: string
}

export interface LoginRequest {
  name: string
  school: string
  class: string
  studentId: string
}

export interface LoginResponse {
  token: string
  userInfo: UserInfo
  expiresIn: number
}

// 测试相关接口类型定义
export interface QuizQuestion {
  id: number
  question: string
  options: string[]
  type: 'single' | 'multiple'
  category: string
}

export interface QuizAnswer {
  questionId: number
  answer: string | string[]
}

export interface QuizResult {
  id: number
  userId: number
  score: number
  category: string
  answers: QuizAnswer[]
  analysis: string
  suggestions: string[]
  completedAt: string
}

export interface QuizReport {
  id: number
  userId: number
  totalScore: number
  categories: {
    name: string
    score: number
    level: string
    description: string
  }[]
  overallAnalysis: string
  recommendations: string[]
  createdAt: string
}

// 用户相关API
export const userApi = {
  // 用户登录/注册
  login: (data: LoginRequest): Promise<ApiResponse<LoginResponse>> => {
    return http.post('/user/login', data, {
      showLoading: true,
      showSuccess: true,
      showError: true
    })
  },

  // 获取用户信息
  getUserInfo: (userId?: number): Promise<ApiResponse<UserInfo>> => {
    return http.get('/user/info', { userId }, {
      showLoading: false,
      showError: true
    })
  },

  // 更新用户信息
  updateUserInfo: (data: Partial<UserInfo>): Promise<ApiResponse<UserInfo>> => {
    return http.put('/user/info', data, {
      showLoading: true,
      showSuccess: true,
      showError: true
    })
  },

  // 上传头像
  uploadAvatar: (file: File): Promise<ApiResponse<{ url: string }>> => {
    return http.upload('/user/avatar', file, {
      showLoading: true,
      showSuccess: true,
      showError: true
    })
  },

  // 用户登出
  logout: (): Promise<ApiResponse<null>> => {
    return http.post('/user/logout', {}, {
      showLoading: true,
      showError: true
    })
  }
}

// 测试相关API
export const quizApi = {
  // 获取测试题目列表
  getQuestions: (category?: string): Promise<ApiResponse<QuizQuestion[]>> => {
    return http.get('/quiz/questions', { category }, {
      showLoading: true,
      showError: true
    })
  },

  // 提交测试答案
  submitAnswers: (data: {
    category: string
    answers: QuizAnswer[]
  }): Promise<ApiResponse<QuizResult>> => {
    return http.post('/quiz/submit', data, {
      showLoading: true,
      showSuccess: true,
      showError: true
    })
  },

  // 获取测试结果
  getResult: (resultId: number): Promise<ApiResponse<QuizResult>> => {
    return http.get(`/quiz/result/${resultId}`, {}, {
      showLoading: true,
      showError: true
    })
  },

  // 获取用户所有测试结果
  getUserResults: (userId?: number): Promise<ApiResponse<QuizResult[]>> => {
    return http.get('/quiz/results', { userId }, {
      showLoading: true,
      showError: true
    })
  },

  // 生成心理报告
  generateReport: (userId?: number): Promise<ApiResponse<QuizReport>> => {
    return http.post('/quiz/report', { userId }, {
      showLoading: true,
      showSuccess: true,
      showError: true
    })
  },

  // 获取心理报告
  getReport: (reportId: number): Promise<ApiResponse<QuizReport>> => {
    return http.get(`/quiz/report/${reportId}`, {}, {
      showLoading: true,
      showError: true
    })
  },

  // 下载报告PDF
  downloadReport: (reportId: number): Promise<void> => {
    return http.download(`/quiz/report/${reportId}/pdf`, {}, `心理测评报告_${reportId}.pdf`)
  }
}

// 统计相关API
export const statsApi = {
  // 获取用户统计数据
  getUserStats: (userId?: number): Promise<ApiResponse<{
    totalTests: number
    completedTests: number
    averageScore: number
    lastTestDate: string
    categories: {
      name: string
      count: number
      averageScore: number
    }[]
  }>> => {
    return http.get('/stats/user', { userId }, {
      showLoading: true,
      showError: true
    })
  },

  // 获取系统统计数据（管理员用）
  getSystemStats: (): Promise<ApiResponse<{
    totalUsers: number
    totalTests: number
    activeUsers: number
    popularCategories: string[]
  }>> => {
    return http.get('/stats/system', {}, {
      showLoading: true,
      showError: true
    })
  }
}

// 文件相关API
export const fileApi = {
  // 上传文件
  upload: (file: File, type: 'image' | 'document' = 'image'): Promise<ApiResponse<{
    url: string
    filename: string
    size: number
  }>> => {
    return http.upload(`/file/upload?type=${type}`, file, {
      showLoading: true,
      showSuccess: true,
      showError: true
    })
  },

  // 删除文件
  delete: (fileUrl: string): Promise<ApiResponse<null>> => {
    return http.delete('/file/delete', {
      data: { url: fileUrl },
      showLoading: true,
      showSuccess: true,
      showError: true
    })
  }
}

// 导出所有API
export default {
  user: userApi,
  quiz: quizApi,
  stats: statsApi,
  file: fileApi
}